import 'package:flutter/cupertino.dart';
import 'package:yi_shop/utils/colors.dart';

class DrawUnderLine extends StatefulWidget {
  final Widget child;

  final Color? color;

  final double dx;

  final double dy;

  const DrawUnderLine({Key? key, required this.child, this.color, this.dx = 0.0, this.dy = 0.0})
      : super(key: key);

  @override
  State<DrawUnderLine> createState() => _DrawUnderLineState();
}

class _DrawUnderLineState extends State<DrawUnderLine> {
  @override
  Widget build(BuildContext context) {
    return CustomPaint(
      painter: _DrawLineColorPainter(
          color: widget.color ?? ColorsUtils.parseColorFromHexString(ColorsUtils.colorF2f2),
          dx: widget.dx,
          dy: widget.dy),
      child: widget.child,
    );
  }
}

class _DrawLineColorPainter extends CustomPainter {
  final Color color;
  final double dx;
  final double dy;

  _DrawLineColorPainter({required this.color, this.dx = 0.0, this.dy = 0.0});

  @override
  void paint(Canvas canvas, Size size) {
    Paint paint = Paint()
      ..style = PaintingStyle.stroke
      ..color = color
      ..strokeWidth = 1;
    canvas.drawLine(Offset(dx, size.height - dy), Offset(size.width - dx, size.height - dy), paint);
  }

  @override
  bool shouldRepaint(covariant _DrawLineColorPainter oldDelegate) => this != oldDelegate;
}
